package com.xia.controller;

import com.xia.dao.article.IArticleDao;
import com.xia.dao.article.ICollectDao;
import com.xia.domian.Collect;
import com.xia.service.ICollectService;
import com.xia.util.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文章控制器
 */
@CrossOrigin
@RestController
@RequestMapping("/collect")
public class CollectController {
    private ICollectService collectArticle;
    private ICollectDao collectDao;
    private IArticleDao articleDao;

    @Autowired
    public void setArticleDao(IArticleDao articleDao) {
        this.articleDao = articleDao;
    }
    @Autowired
    public void setCollectDao(ICollectDao collectDao) {
        this.collectDao = collectDao;
    }
    @Autowired
    public void setCollectArticle(ICollectService collectArticle) {
        this.collectArticle = collectArticle;
    }

    /**
     * 查看指定收藏夹中的帖子
     * @param collectId 收藏夹ID
     */
    @GetMapping("/getCollectArticle/{collectId}")
    public Result getCollectArticle(@PathVariable Integer collectId){
        return Result.success(articleDao.selectCollectArticleUsers(collectId));
    }

    /**
     * 查看用户的收藏列表
     */
    @GetMapping("/selectUsersMsgCollect/{usersId}")
    public Result selectUsersMsgCollect(@PathVariable Integer usersId){
        return Result.success(collectDao.selectCollect(usersId));
    }

    /**
     * 删除收藏夹
     */
    @DeleteMapping("/deleteCollect/{collectId}")
    public Result deleteCollect(@PathVariable Integer collectId){
        collectDao.deleteCollect(collectId);
        return Result.success();
    }

    /**
     * 创建收藏夹/收藏帖子
     */
    @PostMapping("/insertCollect")
    public Result insertCollect(@RequestBody Collect collect){
        List<Collect> collectList=collectArticle.insertCollectArticle(collect);
        return Result.success(collectList);
    }

    /**
     * 获取用户的收藏列表
     */
    @GetMapping("/getCollect/{usersId}/{articleId}")
    public Result getCollect(@PathVariable Integer usersId,@PathVariable Integer articleId){
        return Result.success(collectArticle.collectArticle(usersId,articleId));
    }

    /**
     * 删除收藏夹中帖子
     * @param articleId 帖子ID
     */
    @DeleteMapping("/deleteCollectArticle/{usersId}/{articleId}")
    public Result deleteCollectArticle(@PathVariable Integer usersId,@PathVariable Integer articleId){
        collectDao.deleteCollectArticle(articleId);
        return Result.success(collectArticle.collectArticle(usersId,articleId));
    }
}
